package com.wzh.consumer;

import com.wzh.api.entity.Order;
import com.wzh.api.entity.User;
import com.wzh.api.service.OrderService;
import com.wzh.api.service.UserService;
import com.wzh.core.annotation.RpcConsumer;
import com.wzh.core.config.ConsumerConfig;
import jakarta.annotation.Resource;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@SpringBootApplication
@Import({ConsumerConfig.class})
@RestController
public class RpcDemoConsumerApplication {

    @RpcConsumer
    UserService userService;

    @RpcConsumer
    OrderService orderService;

    public static void main(String[] args) {
        SpringApplication.run(RpcDemoConsumerApplication.class, args);
    }


    @RequestMapping("/")
    public User findUser(Integer id) {
        return userService.findById(id);
    }

    @Bean
    public ApplicationRunner runServiceMethod() {
        return args -> {
//            User user = userService.getUser("UserConsumer");
//            System.out.println(user);

            // 方法重载
//            user = userService.getUser(1234, "UserConsumer");
//            System.out.println(user);
//
//            Integer user2 = userService.findById(2215);
//            System.out.println(user2);
//            System.out.println("============================");
//
//            Order order = orderService.getOrder("OrderConsumer");
//            System.out.println(order);

            // 请求参数为基本数据类型   返回值为基本数据类型  --处理基本类型转化为包装类
//            long longId = userService.findLongId(123L);
//            System.out.println(longId);

            // 请求参数为对象类型 -- 处理 Response泛型 类型转换为 原始类型
//            User user = userService.findUser(new User(1234, "UserBean", 18, "UserBean"));
//            System.out.println(user);

            // 返回值为Long[]
//            Long[] longArray = orderService.getLongArray(new Long[]{123L, 456L, 789L});
//            System.out.println(Arrays.asList(longArray));

            // 返回值为List -- 处理类型不正确问题
//            List<Order> orderList = orderService.getOrderList(
//                    Arrays.asList(
//                            new Order(123, "OrderList1", "OrderList"),
//                            new Order(456, "OrderList2", "OrderList"),
//                            new Order(789, "OrderList3", "OrderList")));
//            System.out.println(orderList);

            // 返回类型为Map
            Map<String, Order> orderMap = orderService.getOrderMap(
                    Stream.of(
                            new Order(123, "OrderMap1", "OrderMap"),
                            new Order(456, "OrderMap2", "OrderMap"),
                            new Order(789, "OrderMap3", "OrderMap"))
                            .collect(Collectors.toMap(Order::getOrderName, order -> order)));
            System.out.println(orderMap);
        };
    }

}
